import { useContext, useEffect, useState } from 'react';
import SettingContext from './index';

/*
 * 封装一个类似connect 的东西
 *
 * */
const getFilterValue = (value, filter) => {
  const arr = [];
  filter.forEach((i) => {
    arr.push(value[i]);
  });
  return arr;
};
// obj = ['name1','name2']
function useConnectSetting(obj = []) {
  const settings = useContext(SettingContext);
  const [value, setValue] = useState({});
  const arry = getFilterValue(settings, obj);
  // 订阅的值变化了，会触发
  useEffect(() => {
    setValue(settings);
  }, arry);

  return value;
}

export default useConnectSetting;
